package com.ybear.citypickerviewdemo;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.ybear.citypickerview.CityPickerDialog;
import com.ybear.citypickerview.CityPickerView;
import com.ybear.citypickerview.TextStyleBuild;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        componentModel();
        dialogModel();
    }

    /**
     * 组件的形式
     */
    private void componentModel() {
        CityPickerView cpvView = findViewById(R.id.main_cpv_city_picker);

//        //设置默认的选择城市内容
//        cpvView.setDefaultSelectCityNames("请选择省份", "请选择城市", "请选择区/县", "请选择街道");

        //选中的图标
        cpvView.setSelectFlag(R.drawable.ic_select_flag);
        //未选中的图标
        cpvView.setUnSelectFlag(R.drawable.ic_unselect_flag);
        //选择城市的Flag图标大小
        cpvView.setFlagSize(50, 50);

        //选择城市的文本颜色
        cpvView.setSelectCityTextColor( R.color.colorWhile );
        //选择Item的文本颜色
        cpvView.setSelectItemTextColor( R.color.colorAccent );
        cpvView.setUnSelectItemTextColor( R.color.colorPrimary );
        //选择城市的背景颜色
        cpvView.setSelectCityBackgroundResource( R.color.colorBlack );

        //设置选择城市的样式（选择省、市、县的按钮）
        cpvView.setSelectCityStyle(new TextStyleBuild()
                .setPadding( 0, 10, 0, 10)
                .setTextSize(16)
                .setTextColor(R.color.colorWhile));

        //设置Item的文本样式（主要用于设置字体和字体大小这些）
        cpvView.setItemStyle(new TextStyleBuild()
                .setTextSize(12)
                //以下部分设置将会无效或者出现问题（选择颜色、按下特效）
                /* .setTextColor(R.color.colorPrimary) */
                /* .setBackgroundResource(R.color.colorWhile) */);

        //处理回调选择的数据
        doCallSelectData( cpvView );
    }

    /**
     * 底部弹框的形式
     */
    private void dialogModel() {
        CityPickerDialog dialog = new CityPickerDialog( this );
        Button showBtn = findViewById(R.id.main_btn_show);
        CityPickerView cpvView;
//
//        //关闭按钮大小
//        dialog.setCloseBtnSize( 50 );
        showBtn.setOnClickListener(view -> {
            if( dialog.isShowing() ) {
                dialog.dismiss();
            }else {
                dialog.show();
            }
        });

        //选择完毕后是否自动关闭。默认：启用
        dialog.setEnableAutoDismiss( true );
        //到达顶部后自动关闭。默认：启用
        dialog.setEnableAutoTopLastDismiss( true );
        //到达顶部后自动关闭的距离。默认：550
        dialog.setAutoTopLastDistance( 550 );

        cpvView = dialog.getCityPickerView();
        //处理回调选择的数据
        doCallSelectData( cpvView );
    }

    /**
     * 处理回调选择的数据
     * @param cpvView   城市选择器
     */
    private void doCallSelectData(CityPickerView cpvView) {
        //实时回调监听器
        cpvView.setOnSelectDataChangeListener((v, data, branch) -> {
            //eg：当前分支：0 -> 数据：CityData{code='11', name='北京市'}
            Log.e("TAG", "当前分支：" + branch + " -> 数据：" + data.toString());
        });

        //完整回调监听器
        cpvView.setOnSelectDataListener((v, data) -> {
            /* eg：CityPickerData{
                province=CityData{code='11', name='北京市'},
                city=CityData{code='1101', name='市辖区'},
                county=CityData{code='110101', name='东城区'},
                street=CityData{code='110101001', name='东华门街道'}} */
            Log.e("TAG", data.toString());
        });
    }
}